#include "FileTools.h"
#include "Tools.h"
#include "StringTools.h"
#include <unistd.h>
#import <UIKit/UIKit.h>
//#import "ThemeManager.h"

char g_sCurPath[MAX_PATH];
char g_sThemePath[MAX_PATH];

void FileTools_Init()
{
	memset(g_sCurPath, 0, sizeof(g_sCurPath));
	
#ifdef HACK_PAG
		NSString *path =  [[NSBundle mainBundle] pathForResource:@"block" ofType:@"data"];
		
		strcpy(g_sCurPath, [path cString]);
		int nPos = StringTools_FindLastChar(g_sCurPath, strlen(g_sCurPath), '/');
		if(nPos>=0)
		{
			g_sCurPath[nPos+1]=0x00;
		}
		else
		{
			strcpy(g_sCurPath, "/private/var/mobile/Library/NetDragon/Applications/Stock_Pro_ax.app/");
		}
#else
		//参数NSDocumentDirectory要获取那种路径
		NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
		NSString *documentsDirectory = [paths objectAtIndex:0];
		
		
		strcpy(g_sCurPath, [documentsDirectory UTF8String]);
		strcat(g_sCurPath, "/");
#endif
}

//void FileTheme_Path()
//{
//  memset(g_sThemePath,0,MAX_PATH);
//  strcpy(g_sThemePath,[[ThemeManager sharedThemeManager].currentThemeHeader cString]);
//  strcat(g_sThemePath, "/");
//  //printf("\n *** %s",g_sThemePath);
//}

void FileTools_CopyToDocuments(char *pFilePath)
{
#ifndef HACK_PAG	
	NSFileManager *fileManager = [NSFileManager defaultManager];
	NSString *unicodePath = [NSString stringWithUTF8String:pFilePath];
	
	{
		char srcRes[MAX_PATH];
		memset(srcRes, 0, sizeof(srcRes));
		
		NSString *pathRes = [[NSBundle mainBundle] pathForResource:@"block" ofType:@"data"];
		strcpy(srcRes, [pathRes cString]);
		int nPos = StringTools_FindLastChar(srcRes, strlen(srcRes), '/');
		if(nPos>=0)
		{
			srcRes[nPos+1]=0x00;
		}
		
		nPos = StringTools_FindLastChar(pFilePath, strlen(pFilePath), '/');
		if(nPos>=0)
		{
			if(strstr(pFilePath,".png"))
			{
				strcat(srcRes, "img/");
			}
			strcat(srcRes, pFilePath+nPos + 1);
		}
		
		NSString *unicodeSrc = [NSString stringWithUTF8String:srcRes];
		
		if([fileManager fileExistsAtPath:unicodeSrc])
		{
			NSData *reader = [NSData dataWithContentsOfFile:unicodeSrc];
			BOOL success = [reader writeToFile:unicodePath atomically:YES];
            DLog(@"写入document%d",success);
		}
	}
#endif
}

void FileTools_UpdateFile(char *pName)
{
	
	char sPath[MAX_PATH];
	memset(sPath, 0, sizeof(sPath));
	FileTools_GetCurrentPath(sPath);
	strcat(sPath, pName);
	
	FileTools_CopyToDocuments(sPath);
}

void FileTools_IsDocuments(char *pFilePath)
{
#ifndef HACK_PAG
	
	NSFileManager *fileManager = [NSFileManager defaultManager];
	NSString *unicodePath = [NSString stringWithUTF8String:pFilePath];
	//lisz 20100810 修改为替换旧语言文件,如果升级安装,语言文件有可能是旧的问题.
	if(![fileManager fileExistsAtPath:unicodePath])
	{
		char srcRes[MAX_PATH];
		memset(srcRes, 0, sizeof(srcRes));
		
		NSString *pathRes = [[NSBundle mainBundle] pathForResource:@"block" ofType:@"data"];
		strcpy(srcRes, [pathRes cString]);
		int nPos = StringTools_FindLastChar(srcRes, strlen(srcRes), '/');
		if(nPos>=0)
		{
			srcRes[nPos+1]=0x00;
		}
		
		nPos = StringTools_FindLastChar(pFilePath, strlen(pFilePath), '/');
		if(nPos>=0)
		{
			if(strstr(pFilePath,".png"))
			{
				strcat(srcRes, "img/");
			}
			strcat(srcRes, pFilePath+nPos);
		}
		
		NSString *unicodeSrc = [NSString stringWithUTF8String:srcRes];
		
		if([fileManager fileExistsAtPath:unicodeSrc])
		{
			NSData *reader = [NSData dataWithContentsOfFile:unicodeSrc];
			[reader writeToFile:unicodePath atomically:YES];
		}
	}
	
#endif
	
}
/**
 * 功能:
 *			获得指定文件大小
 * 参数:
 *			char		*pFilePath			文件路径
 * 返回值:
 *			int		文件大小,如果文件不存在则返回-1
 */
int FileTools_GetFileSize(char *pFilePath)
{
	FileTools_IsDocuments(pFilePath);
	int nRet;
	FILE * pFile;
	//初始为-1
	nRet = -1;
	//以只读方式打开文件
	pFile=fopen(pFilePath, "rb");
	if(pFile)
	{
		//移动指针到文件尾
		fseek(pFile,0,SEEK_END);
		//获得指针位置
		nRet = ftell(pFile);
		//关闭文件
		fclose(pFile);
	}
	return nRet;
}

FILE_DATA* FileTools_ReadDataEx(char *pFilePath)
{
	char sPath[MAX_PATH];
	memset(sPath, 0, sizeof(sPath));
	FileTools_GetCurrentPath(sPath);
	strcat(sPath, pFilePath);
	return FileTools_ReadData(sPath);
}

/**
 * 功能:
 *			读取指定文件,到指定BUF
 * 参数:
 *			char		*pFilePath			文件路径
 * 返回值:
 *			FILE_DATA*		文件数据结构
 */
FILE_DATA* FileTools_ReadData(char *pFilePath)
{
	FileTools_IsDocuments(pFilePath);
	
	FILE_DATA *pFileData;
	int nPos;
	FILE *pFile;
	
	pFileData = (FILE_DATA*)Tools_Malloc(sizeof(FILE_DATA));
	pFileData->nSize = FileTools_GetFileSize(pFilePath);
	if (pFileData->nSize < 0)
	{
		Tools_Free((void **)&pFileData);
		return NULL;
	}
	else if (pFileData->nSize == 0)
	{
		return pFileData;
	}
	pFileData->pData = (char*)Tools_Malloc(pFileData->nSize+2);
	
	nPos = -1;
	//以只读方式打开文件
	pFile = fopen(pFilePath, "rb");
	if(pFile)
	{
		//移动文件指针到文件头
		fseek(pFile, 0, SEEK_SET);
		//如果不是文件尾
		while(!feof(pFile))
		{
			nPos++;
			//读一字节
			fread(pFileData->pData+nPos,1,1,pFile);
		}
		//实际读取的数据大小
		pFileData->nSize = nPos;
		//关闭文件
		fclose(pFile);
	}
	else
	{
		Tools_Free((void **)&pFileData);
		Tools_Free((void **)&pFileData->pData);
		return NULL;
	}
	return pFileData;
}

FILE_DATA* FileTools_ReadTheme(char *pFilePath)
{
  char sPath[MAX_PATH];
  memset(sPath, 0, sizeof(sPath));
  FileTools_GetThemePath(sPath);
  strcat(sPath, pFilePath);
  return FileTools_ReadData(sPath);
}

/**
 * 功能:
 *			释放文件数据
 * 参数:
 *			FILE_DATA		*pFileData		文件结构数据
 * 返回值:
 *			FILE_DATA*		文件数据结构
 */
void FileTools_FreeData(FILE_DATA *pFileData)
{
	if (!pFileData)
	{
		return;
	}
	if (pFileData->pData)
	{
		Tools_Free((void**)&pFileData->pData);
 	}
	
	Tools_Free((void**)&pFileData);
}

TBOOL FileTools_WriteDataEx(char *pFilePath, char *pData, int nSize)
{
	char sPath[MAX_PATH];
	memset(sPath, 0, sizeof(sPath));
	FileTools_GetCurrentPath(sPath);
	strcat(sPath, pFilePath);
	return FileTools_WriteData(sPath, pData, nSize);
}
/**
 * 功能:
 *			将指定BUF内容写入指定文件
 * 参数:
 *			char		*pFilePath			文件路径
 *			char		*pData				BUF地址
 *			int		nSize				BUF大小
 * 返回值:
 *	TBOOL		写入成功：TRUE 写入失败:FALSE
 */
TBOOL FileTools_WriteData(char *pFilePath, char *pData, int nSize)
{
	FILE *pFile;
	//以写方式打开文件
	pFile=fopen(pFilePath,"wb");
	if(pFile)
	{
		//移动指针到文件头
		fseek(pFile,0,SEEK_SET);
		//写入数据
		fwrite(pData,nSize,1,pFile);
		//关闭文件
		fclose(pFile);
		return TRUE;
	}
	return FALSE;
}

void FileTools_DelFile(char *pName)
{
	char sPath[MAX_PATH];
	memset(sPath, 0, sizeof(sPath));
	FileTools_GetCurrentPath(sPath);
	strcat(sPath, pName);
	remove(sPath);
}

/**
 * 功能:
 *			获得程序的绝对路径
 * 参数:
 *			char		*pFilePath			程序路径
 *			 
 *			 
 * 返回值:
 *	 -
 */

void FileTools_GetCurrentPath(char *pFilePaht)
{
	strcpy(pFilePaht, g_sCurPath);
}

void FileTools_GetThemePath(char *pFilePath)
{
  strcpy(pFilePath, g_sThemePath);
}
